package com.ulook.lookplanet.ui.notifications.chat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.ulook.lookplanet.R;
import com.ulook.lookplanet.data.model.TSysMsg;
import com.ulook.lookplanet.service.SysMsgService;
import com.ulook.lookplanet.widget.YouMsgView;

import java.util.List;

public class SyschatActivity extends AppCompatActivity {

    private SyschatViewModel viewModel;
    private LinearLayout msgLayout;
    private LocalBroadcastManager localBroadcastManager;
    private SyschatMsgReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_syschat);

        viewModel = new ViewModelProvider(this).get(SyschatViewModel.class);
        viewModel.getToastText().observe(this, s -> {
            Toast.makeText(SyschatActivity.this, s, Toast.LENGTH_SHORT).show();
        });

        Intent intent = getIntent();
        int currUserId = intent.getIntExtra("currUserId", 0);
        if(currUserId == 0){
            finish();
        }else {
            viewModel.setCurrUserId(currUserId);
        }

        initTopBar();
        initData();

        //  注册广播
        registerMsgReceive();

    }

    @Override
    protected void onDestroy() {
        unregisterMsgReceive();
        super.onDestroy();
    }

    private void initTopBar(){
        QMUITopBarLayout topBar = findViewById(R.id.syschat_bar);
        topBar.addLeftBackImageButton().setOnClickListener(view -> {
            finish();
        });
        topBar.setTitle("系统消息");
    }

    private void initData(){
        msgLayout = findViewById(R.id.syschat_msg_layout);
        // 展示初始数据
        List<TSysMsg> history = viewModel.getHistoryMsg(2);
        List<TSysMsg> unread = viewModel.getUnreadMsg();
        for(int i = 0; i < history.size(); i++){
            YouMsgView youMsgView = new YouMsgView(this);
            youMsgView.setHead(R.mipmap.lookplanet);
            youMsgView.setTime(history.get(i).getSendtime().subSequence(5, 16).toString());
            youMsgView.setMsg(history.get(i).getMsg());
            msgLayout.addView(youMsgView, 0);
        }
        for(int i = 0; i < unread.size(); i++){
            YouMsgView youMsgView = new YouMsgView(this);
            youMsgView.setHead(R.mipmap.lookplanet);
            youMsgView.setTime(unread.get(i).getSendtime().subSequence(5, 16).toString());
            youMsgView.setMsg(unread.get(i).getMsg());
            msgLayout.addView(youMsgView);
        }
    }

    /**
     * 展示新数据
     */
    private void displayNewMsg(TSysMsg tSysMsg){
        YouMsgView youMsgView = new YouMsgView(this);
        youMsgView.setHead(R.mipmap.lookplanet);
        youMsgView.setTime(tSysMsg.getSendtime().subSequence(5, 16).toString());
        youMsgView.setMsg(tSysMsg.getMsg());
        msgLayout.addView(youMsgView);
        viewModel.updateMsgRead(tSysMsg.getUid());
    }

    /**
     * 注册MsgService的广播
     */
    private void registerMsgReceive(){
        //Log.d("#####","注册MsgService的广播");
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        // 用户消息广播
        IntentFilter userFilter = new IntentFilter();
        userFilter.addAction(SysMsgService.INTENT_ACTION);
        receiver = new SyschatActivity.SyschatMsgReceiver();
        localBroadcastManager.registerReceiver(receiver, userFilter);
    }

    /**
     * 注销广播
     */
    private void unregisterMsgReceive(){
        localBroadcastManager.unregisterReceiver(receiver);
    }

    class SyschatMsgReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            TSysMsg tSysMsg = (TSysMsg) intent.getSerializableExtra("msg");
            displayNewMsg(tSysMsg);
        }
    }
}